// xc/components/diy/xcimglistdatav2/xcimglistdatav2.js
const cart = require('../../../sys/cart')
var pro_activitylimit = require('../../../model/pro_activitylimit')
var behaviorsbasic = require('../../behavior/basic') 
Component({
  behaviors: [behaviorsbasic],
  options: {
    addGlobalClass: true,
    multipleSlots: true
  },
  properties: {
    activecartnum:{
      type:Number,
      default:0
    },
    title: {
      type: Object,
      default: ''
    }, 
    objmodel: {
      type: Object,
      default: ''
    }, 
    datasource: {
      type: Object,
      
      default: {}
    }, 
    cartpro:{
      type:Object,
      default:null,
      observer(newval,oldval){

        var temnum=0
        if(newval!==null)
        {
          
          if(this.data.datasource.data&&this.data.datasource.data.id)
          {
            var activeid= this.data.datasource.data.id;          
            if(newval[activeid]){
              for(var a in newval[activeid] ){         
                if(newval[activeid][a])
                {
                  temnum+= parseInt(newval[activeid][a]) 
                } 
              }
            }  
          }
        }
        if(temnum!=this.data.activecartnum){
          this.setData({activecartnum:temnum})
        }
      }
    },
  },
  /**
   * 组件的初始数据
   */
  data: {
    intervalid:-1,
    aunttime:null,
    prolist:[],
    htlimit:null,
    activecartnum:0,

    dotnums: 0,
    scrollleft: 0,
    syswidth: 375,
    scrollWidth: 0,

  },
 
  // lifetimes: {
  //   attached: function() {
  //     // 在组件实例进入页面节点树时执行
  //     this.autoexcel();
  //   },
  //   detached: function() {
  //     // 在组件实例被从页面节点树移除时执行
  //     this.clearauto();
  //   },
  //   show: function() {
  //     // 页面被展示
  //     this.autoexcel();
  //   },
  //   hide: function() {
  //     this.clearauto();
  //     // 页面被隐藏
  //   },
  // },
  ready: function () {
    var that = this;


   if(this.data.datasource.data &&this.data.datasource.data.id)
   {
    pro_activitylimit.activitylimit(this.data.datasource.data.id ).then(function(item){            
      var reslut= item.data;
      if(reslut["status"]==1){
        if(item.data.obj)
        {
        
         that.setData({htlimit:item.data.obj})
        }
        else
        {
          that.setData({htlimit:{}})
        }
        
      }
    })
   }
  
   this.setData({
    scrollleft: 1,
    syswidth: wx.getSystemInfoSync().windowWidth
  })
 
//activitylimit
 
    // this.autoexcel();
  },
  /**
   * 组件的方法列表
   */
  methods: {
    initsiper(ev) {      
      var scrollWidth = ev.detail.scrollWidth
      var syswidth = this.data.syswidth
      var mod_val = scrollWidth % syswidth
      var dotnums = (scrollWidth - mod_val) / syswidth + (mod_val > 0 ? 1 : 0)      
      this.setData({ scrollleft: 0, scrollWidth, dotnums })
    },
    changescrollleft(ev) {
      switch (ev.op) {
        case "setleft": {
          var scrollleft = this.data.syswidth * ev.idx
          if (this.data.scrollleft == scrollleft) {
            scrollleft--
          }
          this.setData({ scrollleft: scrollleft })
          break;
        }
      }
    },

    
    noevent(ev){
      //用来阻止用的， 其它没什么用处
   
   
       },

       /**
        *  购物车操作
        * @param {*} ev 
        */
       cartop(ev){ 
    
        var that = this;
        var activeid=  this.data.datasource.data.id     
      
        
        if(this.data.htlimit==null)
        {
          pro_activitylimit.activitylimit(activeid).then(function(item){            
            var reslut= item.data;
            if(reslut["status"]==1){
              if(item.data.obj)
              {
                that.setData({htlimit:item.data.obj})
              }
              else
              {
                that.setData({htlimit:{}})
              }
              that.cartopnext(ev,activeid)
            }
          })
        }
        else
        {
          that.cartopnext(ev)
        }     
      },
      cartopnext(ev){         
        var detail = ev.detail;
        var item =detail.item;  
       var activemodel= this.data.datasource.data;    
       var activeid=  activemodel.id

        var htlimit= this.data.htlimit;
        var cartpro= this.data.cartpro
    

        switch(detail.op){
          case "setnum":{
            var keyval=item.id+"_1_"+activeid+"_-1_1";
            cart.setnum(keyval,detail.num,true);
            break;
          }
          case "add":{
            var buynum=0
            if(htlimit[item.id]){

              buynum+= parseInt(htlimit[item.id])
            }
            if(cartpro&&cartpro[activeid]&&cartpro[activeid][item.id]){

              buynum+= parseInt(cartpro[activeid][item.id])
            }
            if( item.stock<1){
              wx.showToast({
                title: '没有库存',
              })
              return 
            }           
            if((item.limitnum>0)&&buynum>=item.limitnum){
              wx.showToast({
                title: '限购'+item.limitnum+"件",
              })

              return ;

            }
 
          var  prolimit=item.limitnum;
          if(prolimit>0)
          {
            prolimit-=buynum;
          }


            var maxbuy =activemodel.maxbuy

            var totallimit=-1
            if(maxbuy>0)
            {
              totallimit =maxbuy;
             
               if(  cartpro[activeid]){
                 for(var a in cartpro[activeid])
                 {
                  totallimit-= parseInt(cartpro[activeid][a]) 
                 }
                 
               }

               if(htlimit){
                 for(var tema in htlimit)
                 {
                  totallimit-= parseInt(htlimit[tema]) 
                 }

               }



               if(totallimit<=0){
                wx.showToast({
                  title: '该活动总限购'+maxbuy+"件",
                })
                return ;
               }
            

            } 

          var itype = this.data.objmodel.itype
         
            if( item.guigetype=="-1"&& itype!=2){       


              item["num"]=1
        
              cart.cartadd(item,1,activeid)
            }
            else
            {
           
              detail["atype"]=1

              if(itype)
              {
                detail["atype"]=itype;
              }
              detail["aid"]=activeid
              detail["totallimit"]=totallimit;
              detail["prolimit"]=prolimit;
              
              this.triggerEvent('buy',{dataset:detail} )
            }
            break;
  
          }
          
        }
        this.triggerEvent('cartop', {op:"reload"})        
      },
    buy(ev){

   
   
     var currentTarget= ev.currentTarget
     

 

 
 
     
      this.triggerEvent('buy', currentTarget)
    },
    cartchose(e){        
      this.triggerEvent("cartchose",e.detail);
    },
    clearauto: function () {
      var that = this;
      if (that.data.intervalid != -1) {
        clearInterval(that.data.intervalid)
        that.data.intervalid = -1;
  
      }
    },
    autoexcel: function (rest) {
      var that = this;  

      if(!rest){
         if(that.data.intervalid!=-1){
           return ;
         }
      }
      else
      {
        this.clearauto();
      }     
      that.data.intervalid = setInterval(function () {
        
        that.setData({aunttime:new Date()})        
      }, 1000)
  
  
    },

  }
})
